Instagram Scraper using Flask

Star Badge Open Source Love

Instragam Profile Scraper in Flask

🛠️ Description

This project is about developing a an API in Flask using Instaloader and Instagram’s GraphQL API’s to scrape the no of likes and comments of all of the post of a public instagram profile.

⚙️ Languages or Frameworks Used

  • Flask
  • Instaloader, Instgram GraphQL APIs

🌟 How to run

  • Install all the requirements

    Run pip install -r requirements.txt to install all the requirements.

  • Now Just, Run the project

    • To the run the project, go to the bash terminal of VSCode or any other code editor and run ./start_server.sh.
    • The server would start running on http://127.0.0.1:{port_number}.(generally http://127.0.0.1:5000)
  • Explore the API

    Go to the browser/postman/thunderclient and hit the following URL http://127.0.0.1:5000/get_profile/{instagram_username} > Note: The Instagram Profile must be public.

📺 Demo

  • Main screen of the application. image
  • Result for my instagram account (@mbsaiaditya). image

🤖 Author

Github - MBSA-INFINITY LinkedIn - MBSAIADITYA Portfolio - MBSA Instagram - MBSAIADITYA

Source Code: app.py

from flask import Flask, request, jsonify
from helper import get_all_posts, scrape_user_id
import instaloader

app = Flask(__name__)
insta = instaloader.Instaloader()

@app.route('/', methods=['GET'])
def start():
    return "Instragram Scraper Server is Running!!"

@app.route('/get_profile/<username>', methods=['GET'])
def get_instagram_profile(username):
    try:
        profile = instaloader.Profile.from_username(insta.context, username)
        #Get 
        user_id = scrape_user_id(username)
        # Get post data for all posts
        post_data = get_all_posts(user_id)
        response = {
            "Username": profile.username,
            "Number Of Posts": profile.mediacount,
            "Posts": post_data
        }
        return jsonify(response)
    except instaloader.exceptions.ProfileNotExistsException:
        return jsonify({"error": "Profile does not exist"}), 404
    except instaloader.exceptions.InstaloaderException as e:
        return jsonify({"error": f"An error occurred: {str(e)}"}), 500
    except Exception as e:
        return jsonify({"error": f"{str(e)}"}), 400


if __name__ == '__main__':
    app.run()